//
//  ViewUtil.swift
//  HFUTER
//
//  Created by HD on 15/1/17.
//  Copyright (c) 2015年 HD. All rights reserved.
//

import UIKit

class ViewUtil {
   
    /// 移除一个View的所有子View
    class func removeAllViews(view: UIView) {
        view.subviews.map({(subview: AnyObject)-> AnyObject in
            if subview.isKindOfClass(UIView) {
                subview.removeFromSuperview()
            }
            return subview
        })

    }
    
    /// 从一个xib文件中初始化UIViewController
    class func instanceViewController(name: String) -> UIViewController {
        let nibView = NSBundle.mainBundle().loadNibNamed(name, owner: nil, options: nil)
        return nibView.first as! UIViewController
    }
    
    /// 从一个xib文件中初始化UIView
    class func instanceView(name: String)-> UIView {
        let nibView = NSBundle.mainBundle().loadNibNamed(name, owner: nil, options: nil)
        return nibView.first as! UIView
    }
    
    /// 从storyboard加载UIView
    class func instanceView(storyboard: String, identifier: String) -> UIView! {
        let mainStoryboard = UIStoryboard(name: storyboard, bundle: NSBundle.mainBundle())
        return mainStoryboard.instantiateViewControllerWithIdentifier(identifier) as! UIView
    }
    
    /// 从storyboard加载UIViewController
    class func instanceViewController(storyboard: String, identifier: String) -> UIViewController! {
        return UIStoryboard(name: storyboard, bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier(identifier) as! UIViewController
    }
    
}
